package com.uwang.library.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.uwang.library.dao.BorrowedRecordDao;
import com.uwang.library.model.BeOverdueModel;
import com.uwang.library.model.MemberListModel;
import com.uwang.library.util.DruidUtil;

public class BorrowedRecordDaoImpl implements BorrowedRecordDao {

	@Override
	public List<BeOverdueModel> bomList() {
		PreparedStatement preparedStatement;
		ResultSet resultSet = null;
		List<BeOverdueModel> bomList = new ArrayList<>();
			
		try {
			Connection connection= DruidUtil.getConnection();
			String sql = "select b.id,bk.id bookId,mb.user_name userName,mb.user_phone userPhone,\r\n" + 
						 "bk.bk_name bookName,b.borrowing_time borrowingTime,\r\n" + 
						 "b.term,b.`status` from borrowing b\r\n" + 
						 "left join member mb on mb.id = b.user_id\r\n" + 
						 "left join books bk on bk.id = b.book_id";
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				BeOverdueModel beOverdueModel = new BeOverdueModel();
				beOverdueModel.setId(resultSet.getInt("id"));
				beOverdueModel.setBookId(resultSet.getInt("bookId"));
				beOverdueModel.setUserName(resultSet.getString("userName"));
				beOverdueModel.setUserPhone(resultSet.getString("userPhone"));
				beOverdueModel.setBookName(resultSet.getString("bookName"));
				beOverdueModel.setBorrowingTime(resultSet.getTimestamp("borrowingTime"));
				beOverdueModel.setTerm(resultSet.getTimestamp("term"));
				beOverdueModel.setStatus(resultSet.getInt("status"));
				bomList.add(beOverdueModel); 
				} 
			}catch(SQLException e) {
				e.printStackTrace();
			}
		return bomList;
	}

	@Override
	public void deletionRecord(int borrowId) {
		
		Connection connection = DruidUtil.getConnection();
		PreparedStatement PreparedStatement;
		String sql = "DELETE FROM borrowing WHERE id=?";
		try {
			PreparedStatement= connection.prepareStatement(sql);
			PreparedStatement.setInt(1, borrowId);
			PreparedStatement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
